import { createRouter, createWebHistory } from 'vue-router'
import wangView from '../views/wang/index.vue'
import listView from '../views/list/index.vue'
import NotFound from '../views/404/index.vue'

import cabe from '../views/list/children/cabe.vue'
import tabe from '../views/list/children/tabe.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [

     {
      path: '/',
        redirect :"/wang"
      },
    {
      path: '/wang',
      name: 'wang',
      component: wangView,
    },

    {
      path: '/list',
      name: 'list',
      component: listView,

      children: [
        {
          path: 'tabe',
          name: 'tabe',
          component: tabe,
        },
        {
          path: 'cabe',
          name: 'cabe',
          component: cabe,
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: NotFound
    },
  ],
})


// GOOD
router.beforeEach((to, from, next) => {
  let isAuthenticated = localStorage.getItem("token")
  if (to.name !== 'wang') {
    if (!isAuthenticated) {

      next({ name: 'wang' })
    }
    else {
      next()
    }
  }
  else {
    next()
  }
})
export default router
